﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace JsonToSqlPreview
{
    /// <summary>
    /// Main form of preview application
    /// </summary>
    public partial class MainForm : Form
    {
        /// <summary>
        /// Constructor of form
        /// </summary>
        public MainForm()
        {
            InitializeComponent();
        }
        string result;
        private void btnCommit_Click(object sender, EventArgs e)
        {
            try
            {
                btnResults.Visible = false;
                lblStatus.Text = "Executing ....";
                JsonToSql.JsonToSqlProvider provider = new JsonToSql.JsonToSqlProvider(txtJsonInput.Text);
                result = provider.Commit(txtConnectionString.Text);
                if (result != null)
                {
                    lblStatus.Text = "Ready, go to results";
                    ResultForm rf = new ResultForm(result);
                    rf.ShowDialog();
                    btnResults.Visible = true;
                }
                else
                {
                    lblStatus.Text = "Ready";
                }
               
            }
            catch (Exception ex)
            {
                lblStatus.Text = ex.Message;
            }
        }

        private void btnResults_Click(object sender, EventArgs e)
        {
            ResultForm rf = new ResultForm(result);
            rf.ShowDialog();
        }

        private void openMSSQLStudio()
        {
            try
            {
                System.Data.SqlClient.SqlConnectionStringBuilder csb = new System.Data.SqlClient.SqlConnectionStringBuilder(txtConnectionString.Text);
                string args = string.Format("-S {0} -d {1}", csb.DataSource, csb.InitialCatalog);
                if (csb.IntegratedSecurity)
                {
                    args += " -E";
                }
                else
                {
                    args += string.Format(" -U {0} -P {1}", csb.UserID, csb.Password);
                }
                ProcessStartInfo psi = new ProcessStartInfo("ssms.exe", args);
                Process.Start(psi);
            }
            catch (Exception ex)
            {
                lblStatus.Text = ex.Message;
                goToExpressDownload();
            }
        }

        private void linkExpress_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            goToExpressDownload();
        }

        private static void goToExpressDownload()
        {
            ProcessStartInfo psi = new ProcessStartInfo("https://www.microsoft.com/en-us/download/details.aspx?id=42299");
            Process.Start(psi);
        }

        private void linkSMO_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            ProcessStartInfo psi = new ProcessStartInfo("https://www.microsoft.com/en-us/download/details.aspx?id=42295");
            Process.Start(psi);
        }

        private void btnMSSQL_Click(object sender, EventArgs e)
        {
            openMSSQLStudio();
        }
    }
}
